home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / forms / sticky / sticky.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-01-11  |  7.3 KB  |  263 lines

  1. VERSION 2.00
  2. Begin Form frmSticky 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    ClientHeight    =   1500
  6.    ClientLeft      =   930
  7.    ClientTop       =   1410
  8.    ClientWidth     =   7245
  9.    ControlBox      =   0   'False
  10.    Height          =   1905
  11.    Left            =   870
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    MinButton       =   0   'False
  15.    ScaleHeight     =   1500
  16.    ScaleWidth      =   7245
  17.    Top             =   1065
  18.    Width           =   7365
  19.    Begin CommandButton Button 
  20.       Height          =   615
  21.       Index           =   2
  22.       Left            =   2640
  23.       TabIndex        =   2
  24.       Top             =   360
  25.       Width           =   975
  26.    End
  27.    Begin CommandButton Button 
  28.       Height          =   615
  29.       Index           =   1
  30.       Left            =   1440
  31.       TabIndex        =   1
  32.       Top             =   360
  33.       Width           =   975
  34.    End
  35.    Begin CommandButton Button 
  36.       Height          =   615
  37.       Index           =   0
  38.       Left            =   240
  39.       TabIndex        =   0
  40.       Top             =   360
  41.       Width           =   975
  42.    End
  43. Option Explicit
  44. ' This form is a "sticky" form. i.e. it acts like the
  45. ' Win95 taskbar. When dragged, it "sticks" to the nearest
  46. ' side of the screen.
  47. ' VERT_WIDTH & HORZ_HEIGHT define the size of
  48. ' the form in each position.
  49. ' BUTTON_WIDTH defines the width of the buttons when horz,
  50. ' and the height when vert.
  51. ' MoveType (assigned in Form_Load) specifies how the move
  52. ' is done:
  53. '   MT_FORM: The whole form is re-drawn as you drag it.
  54. '   MT_RECT: Only a rectangle is drawn while dragging, the
  55. '            form is re-drawn only when mouse released.
  56. ' Accuracy (assigned in Form_Load) specifies how close
  57. ' the mouse must be to any side to switch positions:
  58. '   ACC_EXACT: The mouse must be in the rectangle where
  59. '              the form would be.
  60. '   ACC_NEAREST: The side selected is the side the mouse
  61. '                is closest to. (As per the Win95 taskbar)
  62. Const MT_RECT = 1
  63. Const MT_FORM = 2
  64. Dim MoveType As Integer
  65. Dim RectPos As Integer
  66. Const ACC_EXACT = 1
  67. Const ACC_NEAREST = 2
  68. Dim Accuracy As Integer
  69. Dim CurrentPos As Integer
  70. Const POS_NONE = 0
  71. Const POS_TOP = 1
  72. Const POS_BOTTOM = 2
  73. Const POS_LEFT = 3
  74. Const POS_RIGHT = 4
  75. Const LEFT_BUTTON = 1
  76. Const VERT_WIDTH = 1000     ' twips
  77. Const HORZ_HEIGHT = 1000    ' twips
  78. Const BUTTON_WIDTH = 1000   ' twips
  79. Sub Button_Click (Index As Integer)
  80.     End
  81. End Sub
  82. Sub CalcPosition (CalcPos As Integer, x1!, x2!, y1!, y2!)
  83.     Select Case CalcPos
  84.     Case POS_TOP
  85.     x1! = 0
  86.     y1! = 0
  87.     x2! = screen.Width
  88.     y2! = HORZ_HEIGHT
  89.     Case POS_BOTTOM
  90.     x1! = 0
  91.     y1! = screen.Height - HORZ_HEIGHT
  92.     x2! = screen.Width
  93.     y2! = screen.Height
  94.     Case POS_LEFT
  95.     x1! = 0
  96.     y1! = 0
  97.     x2! = VERT_WIDTH
  98.     y2! = screen.Height
  99.     Case POS_RIGHT
  100.     x1! = screen.Width - VERT_WIDTH
  101.     y1! = 0
  102.     x2! = screen.Width
  103.     y2! = screen.Height
  104.     End Select
  105. End Sub
  106. Sub DrawFormHorizontal ()
  107. Dim i As Integer
  108.     For i = 0 To 2
  109.     Button(i).Left = i * BUTTON_WIDTH * 1.5 + (BUTTON_WIDTH * .25)
  110.     Button(i).Top = Me.Height / 4
  111.     Button(i).Width = BUTTON_WIDTH
  112.     Button(i).Height = Me.Height / 2
  113.     Next i
  114. End Sub
  115. Sub DrawFormVertical ()
  116. Dim i As Integer
  117.     For i = 0 To 2
  118.     Button(i).Top = i * BUTTON_WIDTH * 1.5 + (BUTTON_WIDTH * .25)
  119.     Button(i).Left = Me.Width / 4
  120.     Button(i).Height = BUTTON_WIDTH
  121.     Button(i).Width = Me.Width / 2
  122.     Next i
  123. End Sub
  124. Sub DrawRect (DrawPos As Integer)
  125. Dim x1!, x2!, y1!, y2!, hDC%, RectRect As Rect
  126.     If DrawPos <> RectPos Then
  127.     hDC% = GetScreenDC(0)
  128.     If hDC% Then
  129.         
  130.         ' Note: DrawFocusRect uses PIXELS !
  131.         If RectPos <> POS_NONE Then
  132.         ' First, un-draw previous rect
  133.         CalcPosition RectPos, x1!, x2!, y1!, y2!
  134.         RectRect.Left = CInt(x1! / screen.TwipsPerPixelX)
  135.         RectRect.Top = CInt(y1! / screen.TwipsPerPixelY)
  136.         RectRect.right = CInt(x2! / screen.TwipsPerPixelX)
  137.         RectRect.bottom = CInt(y2! / screen.TwipsPerPixelY)
  138.         DrawFocusRect hDC%, RectRect
  139.         End If
  140.         If DrawPos <> POS_NONE Then
  141.         ' Then draw new one
  142.         CalcPosition DrawPos, x1!, x2!, y1!, y2!
  143.         RectRect.Left = CInt(x1! / screen.TwipsPerPixelX)
  144.         RectRect.Top = CInt(y1! / screen.TwipsPerPixelY)
  145.         RectRect.right = CInt(x2! / screen.TwipsPerPixelX)
  146.         RectRect.bottom = CInt(y2! / screen.TwipsPerPixelY)
  147.         DrawFocusRect hDC%, RectRect
  148.         End If
  149.         
  150.         hDC% = ReleaseScreenDC(0, hDC%)
  151.     End If
  152.     RectPos = DrawPos
  153.     End If
  154. End Sub
  155. Sub Form_Load ()
  156.     CurrentPos = POS_NONE
  157.     RectPos = POS_NONE
  158.     MoveType = MT_RECT
  159.     Accuracy = ACC_NEAREST
  160.     SetPosition POS_BOTTOM
  161. End Sub
  162. Sub Form_MouseDown (Button As Integer, Shift As Integer, x As Single, y As Single)
  163.     If MoveType = MT_RECT Then
  164.     DrawRect CurrentPos
  165.     End If
  166. End Sub
  167. Sub Form_MouseMove (Button As Integer, Shift As Integer, x As Single, y As Single)
  168. Dim sx!, sy!, NewPos As Integer
  169. Dim x1!, x2!, y1!, y2!, mindiff!
  170.     If (Button And LEFT_BUTTON) Then
  171.     sx! = x + Me.Left
  172.     sy! = y + Me.Top
  173.     NewPos = POS_NONE
  174.     Select Case Accuracy
  175.     Case ACC_NEAREST
  176.         x1! = Abs(sx!) * HORZ_HEIGHT
  177.         x2! = Abs(screen.Width - sx!) * HORZ_HEIGHT
  178.         y1! = Abs(sy!) * VERT_WIDTH
  179.         y2! = Abs(screen.Height - sy!) * VERT_WIDTH
  180.         If x1! < x2! Then
  181.         mindiff! = x1!
  182.         NewPos = POS_LEFT
  183.         Else
  184.         mindiff! = x2!
  185.         NewPos = POS_RIGHT
  186.         End If
  187.         If mindiff! > y1! Then
  188.         mindiff! = y1!
  189.         NewPos = POS_TOP
  190.         End If
  191.         If mindiff! > y2! Then
  192.         mindiff! = y2!
  193.         NewPos = POS_BOTTOM
  194.         End If
  195.     Case ACC_EXACT
  196.         If IsInRect(POS_TOP, sx!, sy!) Then
  197.         NewPos = POS_TOP
  198.         ElseIf IsInRect(POS_BOTTOM, sx!, sy!) Then
  199.         NewPos = POS_BOTTOM
  200.         ElseIf IsInRect(POS_LEFT, sx!, sy!) Then
  201.         NewPos = POS_LEFT
  202.         ElseIf IsInRect(POS_RIGHT, sx!, sy!) Then
  203.         NewPos = POS_RIGHT
  204.         End If
  205.     End Select
  206.     Select Case MoveType
  207.     Case MT_RECT
  208.         If Not IsInRect(RectPos, sx!, sy!) Or Accuracy = ACC_NEAREST Then
  209.         DrawRect NewPos
  210.         End If
  211.     Case MT_FORM
  212.         If Not IsInRect(CurrentPos, sx!, sy!) Or Accuracy = ACC_NEAREST Then
  213.         If NewPos <> POS_NONE Then
  214.             SetPosition NewPos
  215.         End If
  216.         End If
  217.     End Select
  218.     End If
  219. End Sub
  220. Sub Form_MouseUp (Button As Integer, Shift As Integer, x As Single, y As Single)
  221. Dim OldRectPos As Integer
  222.     If MoveType = MT_RECT Then
  223.     OldRectPos = RectPos
  224.     DrawRect POS_NONE
  225.     If OldRectPos <> POS_NONE Then
  226.         SetPosition OldRectPos
  227.     End If
  228.     End If
  229. End Sub
  230. Function IsInRect (CheckPos As Integer, x As Single, y As Single) As Integer
  231. Dim x1!, x2!, y1!, y2!
  232.     CalcPosition CheckPos, x1!, x2!, y1!, y2!
  233.     IsInRect = (x >= x1! And x <= x2! And y >= y1! And y <= y2!)
  234. End Function
  235. Sub SetPosition (NewPos As Integer)
  236. Dim x1!, x2!, y1!, y2!
  237.     If NewPos <> CurrentPos Then
  238.     ' Calculate new spot
  239.     CalcPosition NewPos, x1!, x2!, y1!, y2!
  240.     ' hide window so flicker is minimised
  241.     Me.Hide
  242.     Me.Left = x1!
  243.     Me.Top = y1!
  244.     Me.Width = x2! - x1!
  245.     Me.Height = y2! - y1!
  246.     ' Re-draw objects in form
  247.     Select Case NewPos
  248.     Case POS_TOP
  249.         DrawFormHorizontal
  250.     Case POS_BOTTOM
  251.         DrawFormHorizontal
  252.     Case POS_LEFT
  253.         DrawFormVertical
  254.     Case POS_RIGHT
  255.         DrawFormVertical
  256.     End Select
  257.     ' re-display form
  258.     Me.Show 0
  259.     ' record the new pos
  260.     CurrentPos = NewPos
  261.     End If
  262. End Sub
  263.